#!/bin/sh

# PROVIDE: ix-replfix
# REQUIRE: NETWORKING FILESYSTEMS ix-zfs

. /etc/rc.subr

replication_fix()
{
	local readonly sentinel_dir="/data/sentinels"
	local readonly sentinel_file="${sentinel_dir}/replfix-1"
	local readonly fix_prog="/usr/local/bin/replfix.py"
	local readonly fix_args="-s"
	
	if test -f "${sentinel_file}"; then
		return 0
	elif test -f ${fix_prog}; then
		mkdir -p "${sentinel_dir}" > /dev/null 2>&1 || true
		# Getting rid of the freenas:state property takes too
		# long, so let's do it in the background
		zpool list -H -o name | grep -v freenas-boot |
		    while read pname
		    do
			zfs inherit -r freenas:state "${pname}" &
		    done
		${fix_prog} ${fix_args} && touch "${sentinel_file}"
	fi
	return 0
}

name="ix-replfix"
start_cmd="replication_fix"
stop_cmd=":"

load_rc_config $name
run_rc_command "$1"

